package com.neuedu.jdboot.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.jdboot.bean.Privilege;
import com.neuedu.jdboot.bean.Role;
import com.neuedu.jdboot.common.DataTableParam;
import com.neuedu.jdboot.common.DataTableResult;
import com.neuedu.jdboot.common.JdbootException;
import com.neuedu.jdboot.common.Pager;
import com.neuedu.jdboot.common.Result;
import com.neuedu.jdboot.service.PrivilegeService;
import com.neuedu.jdboot.service.RoleService;

@Controller
@RequestMapping("/sys/role")
public class RoleController {
	private Logger logger = LoggerFactory.getLogger(RoleController.class);
	@Autowired
	private RoleService roleService;

	@Autowired
	private PrivilegeService privilegeService;

	public String list3(Pager<Role> pager, Model model) {

		List<Role> list = roleService.findByPager(null);

		model.addAttribute("list", list);

		return "admin/admin-role";
	}

	@GetMapping("/list")
	public String list() {
		return "admin/admin-role-list";
	}

	@PostMapping("/listall")
	@ResponseBody
	public DataTableResult<Role> listAll(@RequestBody DataTableParam param, Model model) {
		DataTableResult<Role> dataTableResult = new DataTableResult<>();
		logger.info("param:::   " + param.toString());

		// 查询出所有数据，使用datatable客户端进行分页
		Pager<Role> pager = new Pager<>();
		// TODO start和size参数同时使用时算法有bug。需要先设置size再设置start
		pager.setSize(param.getLength());
		pager.setStart(param.getStart());
		pager.setCondition(new Role(null, param.getSearch().getValue(), null, param.getSearch().getValue()));
		logger.info("pager:::   " + pager.toString());

		List<Role> list = roleService.findByPager(pager);
		
		int total = roleService.findTotal(pager);
		
		dataTableResult.setData(list);
		dataTableResult.setRecordsFiltered(total);
		dataTableResult.setRecordsTotal(total);
		dataTableResult.setDraw(param.getDraw());

		logger.info(dataTableResult.toString());
		return dataTableResult;
	}

	@GetMapping("/add")
	public String add(Model model) {
		// 权限树
		List<Privilege> privilegeList = privilegeService.findTreePrivileges();

		model.addAttribute("privilegeList", privilegeList);

		return "admin/admin-role-add";
	}

	@PostMapping("/add")
	@ResponseBody
	public Result doAdd(Role role) {
		Result result = new Result();

		try {
			boolean ret = roleService.add(role);
			if (ret) {
				result.setSuccess(Boolean.TRUE);
				result.setErrorMsg("新增成功");
			}
		} catch (JdbootException e) {
			logger.info(e.getMessage(), e);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	@GetMapping("/edit")
	public String edit() {

		return "admin/admin-role-edit";
	}
}
